package ngat.oss;

import ngat.message.OSS.*;
import ngat.util.*;
import ngat.util.logging.*;
import ngat.phase2.*; 
import ngat.instrument.*;
import ngat.phase2.nonpersist.*;

import java.io.*;
import java.lang.reflect.*;

import jyd.dbms.*;
import jyd.storable.*;
import jyd.collection.*;

/**
 * Action: Adds a Tag to the database.
 * @author S.N.Fraser
 * $Id: ADD_TAGImpl.java,v 1.1 2006/11/27 08:45:00 snf Exp $
 */
public class ADD_TAGImpl extends TransactionImpl {
    
    /** The Tag data to insert into db root.*/
    protected Tag tag;
    
    public ADD_TAGImpl(Tag tag) {
	super();
	this.tag = tag;
    }
    
    public ADD_TAGImpl(ADD_TAG request) throws InvocationTargetException {
	super(request);
	this.tag = request.getTag();	
    }

    /**
     * Places the Tag in the DB.
     * @return result.
     */
    public TRANSACTION_DONE exec() { 

	Transaction t = null;
	try {
	    t = Transaction.current();
 
	    Root root = OSS_Utilities.fetchRoot();
	    
	    // Check if Root exists.
	    if (root == null) {
		logger.log(1, "ADDTAG_Impl", id, "exec",
			   "Root not found in database.");
		return error(TRANSACTION.PATH_RESOLUTION_ERROR,
				     "Could not find DB Root");
	    }
	    
	    
	    // Slot the Tag in.
	    root.addTag(tag);
	    
	    ADD_TAG_DONE done = new ADD_TAG_DONE(id);
	    done.setSuccessful(true);
	    return done;

	} finally {
	    t.commit();
	}
	
    }
    
}

/** $Log: ADD_TAGImpl.java,v $
/** Revision 1.1  2006/11/27 08:45:00  snf
/** Initial revision
/** */
